import BaseView from "../../cfw/mvc/BaseView";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import SDKManager from "../../sdk/SDKManager";
import BagManager from "../bag/BagManager";
import { BannerDir, BattleID } from "../../sdk/SDKConfig";
import CrazyClickC from "./CrazyClickC";
import { InitValue } from "../../public/Config";
import RandomHelper from "../../cfw/tools/RandomHelper";
import { MOVE_DIR } from "../../cfw/tools/Define";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";
let add: number = 15
let sub: number = 12;
let DIRX = 'dirX';
let DIRY = 'dirY'
let SPEED = 'speedX'
let SPEEDY = 'speedY'
const { ccclass, property } = cc._decorator;

@ccclass

export default class CrazyClickView extends BaseView {

	@property({ type: cc.Sprite, displayName: "giftBox$VSprite" })
	giftBox$VSprite: cc.Sprite = null;

	@property(cc.ProgressBar)
	progressBar: cc.ProgressBar = null;

	@property({ type: cc.Button, displayName: "click$VButton" })
	click$VButton: cc.Button = null;

	@property({ type: cc.Button, displayName: "back$VButton" })
	back$VButton: cc.Button = null;

	@property(cc.Node)
	box: cc.Node = null;

	@property(cc.Node)
	chuizi: cc.Node = null;

	@property(cc.Node)
	shells: cc.Node = null;

	@property(cc.Node)
	eggshell: cc.Node = null;

	@property(cc.Node)
	CustomNode: cc.Node = null;

	protected isClick: boolean = false;

	protected process: number = 0;

	protected duration: number = 0;

	protected reduce: number = 1;

	protected isJump: boolean = false;



	protected controller: CrazyClickC;

	protected ballRollCount: number = 0;

	protected step: number = 0;

	protected isOk: boolean = true;

	protected hasAd: boolean = false;

	onLoad() {
		GlobalEvent.instance().on(GlobalEvent.EVENT_SHOW, this.eventShow, this)
		this.rollStart()
		this.progressBar.progress = 0;
		this.shells.active = false;
		this.giftBox$VSprite.node.active = false;
		this.eggshell.active = false;
		// cc.tween(this).to(3, {}).call(() => {
		// this.showEgg()
		// }).start()

	}


	rollStart() {
		for (let index = 0; index < this.box.children.length; index++) {
			const element = this.box.children[index];
			if (element[DIRX] == undefined) {
				element[DIRX] = MOVE_DIR[RandomHelper.random(0, MOVE_DIR.length)];
			}
			if (element[DIRY] == undefined) {
				element[DIRY] = MOVE_DIR[RandomHelper.random(0, MOVE_DIR.length)];
			}
			if (element[SPEED] == undefined) {
				element[SPEED] = RandomHelper.random(15, 20);
			}
			// if (element[SPEEDY] == undefined) {
			element[SPEEDY] = element[SPEED]
			// }
		}
	}
	eventShow() {
		if (!this.hasAd) {
			return;
		}
		// console.log(' CrazyClickView eventShow',)
		this.isJump = true;
		this.clickSuccess()
		SDKManager.getChannel().hideBanner()
		SDKManager.getChannel().loadBanner()
	}

	// rollBox() {
	// 	let pos = this.box.parent.convertToWorldSpaceAR(this.box.position)
	// 	for (let index = 0; index < this.box.children.length; index++) {
	// 		const element = this.box.children[index];
	// 		this.rollAction(element, pos)
	// 	}
	// }

	// rollAction(ball: cc.Node, boxPos: cc.Vec3) {
	// 	let hw = this.box.width / 2;
	// 	let ballpos = ball.parent.convertToWorldSpaceAR(ball.position)
	// 	let dis = RandomHelper.random(20, 60);
	// 	let h = RandomHelper.random(50, 100);
	// 	if (ballpos.x > boxPos.x + hw) {
	// 		dis = -dis;
	// 	}
	// 	let c1 = cc.v2(ball.x + dis, ball.y + h);
	// 	let c2 = cc.v2(ball.x + dis, ball.y + h);
	// 	let c3 = cc.v2(ball.x + dis * 2, ball.y);
	// 	cc.tween(ball).bezierTo(0.5, c1, c2, c3).call(() => {
	// 		this.ballRollCount++;
	// 		if (this.ballRollCount >= this.box.childrenCount * 3) {
	// 			this.showEgg()
	// 		} else {
	// 			this.rollAction(ball, boxPos)
	// 		}

	// 	}).start()



	// }

	showChuizi() {
		cc.Tween.stopAllByTarget(this.chuizi)
		this.chuizi.active = true;
		this.chuizi.angle = -60
		cc.tween(this.chuizi).by(0.1, { angle: 30 }).call(() => {
			this.chuizi.active = false;
		}).start()
	}

	showEgg() {
		this.step = 1;
		this.giftBox$VSprite.node.active = true;
		this.showChuizi()
		this.setTimeout(() => {
			this.showChuizi()
		}, 300);

	}

	eggBreak() {
		this.eggshell.active = true;
		this.shells.active = true;
		this.giftBox$VSprite.node.active = false;
		let list = this.shells.children;
		let count: number = 0;
		for (let index = 0; index < list.length; index++) {
			const element = list[index];
			let tx = MOVE_DIR[RandomHelper.random(0, MOVE_DIR.length)] * RandomHelper.random(50, 150)
			let ty = - RandomHelper.random(200, 250)
			cc.tween(element).to(0.5, { x: tx, y: ty }).call(() => {
				count++;
				// element.opacity = 0;
				if (count >= list.length) {

				}
			}).start()
		}

		this.setTimeout(() => {
			this.showGold()
		}, 500)

	}

	showGold() {
		if (cc.isValid(this.node)) {
			let pos = this.shells.parent.convertToWorldSpaceAR(this.shells.getPosition())
			BagManager.addCoin(InitValue.CRAZY_REWARD_NUM, pos, 1)
		} else {
			BagManager.addCoin(InitValue.CRAZY_REWARD_NUM)
		}
		this.setTimeout(() => {
			this.hide()
			this.controller.next();
		}, 2000)
	}

	clickSuccess() {
		this.step = 2;
		this.eggBreak();
	}

	onDestroy() {
		CrazyClickC.step++;
		SDKManager.getChannel().hideBanner()
		SDKManager.getChannel().hideCustomAd()
		GlobalEvent.instance().off(GlobalEvent.EVENT_SHOW, this.eventShow, this)
		SDKManager.getChannel().battleEnd(BattleID.HIT_LOBBY_EGG_TO_LOBBY)
		ModuleManager.getLoader(ModuleID.CRAZY_CLICK).release()
	}
	protected showCount: number = 0;
	onclick$VButtonClick() {
		if (this.step != 1) {
			return;
		}
		if (this.isJump) {
			return;
		}
		if (!this.isOk) {
			this.isOk = true;
			return;
		}
		this.showChuizi()
		if (!this.isClick) {
			this.isClick = true;
		}
		this.duration = 1;
		this.reduce = 1;
		if (this.process < 100) {
			this.process += add;
			if (this.process > 100) {
				this.process = 100;
				this.clickSuccess()
			}
			if (this.process >= 60) {
				let flag = SDKManager.getChannel().isBannerErrorClick()
				// flag = true;
				if (flag) {
					if (SDKManager.getChannel().hasBanner()) {

						if (!SDKManager.getChannel().getzs_banner_force_click()
							&& this.showCount > 0) {

							return;
						} else {

						}
						this.showCount++;
						let change = SDKManager.getChannel().getzs_advert_change();
						console.log(' change ', change)
						change = parseInt('' + change)
						switch (change) {
							case -1:
								this.showBanner();
								break;
							case 1:
								this.showCustomAd()
								break;
							case 0:
								if (CrazyClickC.step % 2 == 0) {
									this.showBanner();
								} else {
									this.showCustomAd()
								}

								break;

						}

					}

				}

			}
			this.updateProgress()

		}



	}

	showCustomAd() {
		this.isOk = false;
		this.hasAd = true;
		let rx = this.CustomNode.x / this.CustomNode.parent.width;
		// console.log(' this.node.y ', this.node.y, ' this.node.parent.height ', this.node.parent.height)
		let ry = (this.CustomNode.parent.height - this.CustomNode.y) / this.CustomNode.parent.height;
		console.log(' rx == ry ====== ', rx, ry)
		SDKManager.getChannel().showCustomAd(0, rx, ry)
		this.setTimeout(() => {
			this.hasAd = false;
			// if (cc.isValid(this.node)) {
			this.isOk = true;
			if (!this.isJump) {
				SDKManager.getChannel().hideCustomAd()
			}
			// }
		}, 2000);
	}

	showBanner() {
		SDKManager.getChannel().showBanner(0, BannerDir.RIGHT_MID)
		this.isOk = false;
		this.hasAd = true;
		this.setTimeout(() => {
			this.hasAd = false;
			// if (cc.isValid(this.node)) {
			this.isOk = true;
			if (!this.isJump) {
				SDKManager.getChannel().hideBanner()
			}
			// }
		}, 2000);
	}

	updateProgress() {
		this.progressBar.progress = this.process / 100;
	}

	onback$VButtonClick() {
		this.hide()
		this.controller.next();
	}

	update(dt: number) {
		if (this.step == 1) {
			if (this.isClick) {
				if (this.duration > 0) {
					this.duration -= dt;
					if (this.duration < 0) {
						this.duration = 0;
						this.isClick = false;
					}
				}
			} else {
				if (this.reduce > 0) {
					this.reduce -= dt;
					if (this.reduce < 0) {
						this.reduce = 1;
						if (this.process > 0) {
							this.process -= sub;
							if (this.process < 0) {
								this.process = 0;
							}
							this.updateProgress()
						}

					}
				}
			}
		} else if (this.step == 0) {
			// let pos = this.box.parent.convertToWorldSpaceAR(this.box.position)
			this.updateBalls()
		}



	}



	updateBalls() {
		let count: number = 0;
		let max = this.box.children.length;
		for (let index = 0; index < max; index++) {
			const element = this.box.children[index];
			// if (element[SPEED] < 0) {
			// 	continue;
			// }
			let ballpos = element.position;
			let hw = element.width / 2;
			let hh = element.height / 2;




			if (element[SPEED] > 0) {
				if (ballpos.x - hw <= 0 || ballpos.x + hw >= this.box.width) {
					element[DIRX] = -element[DIRX]
					element[SPEED] -= 1;
				}
				element.x += element[DIRX] * element[SPEED]
				if (element.y - hh <= 0) {
					element[SPEED] -= 0.1;
				}


			}

			if (element[SPEEDY] > 0) {
				if (ballpos.y - hh <= 0 || ballpos.y + hh >= this.box.height) {
					element[DIRY] = -element[DIRY]
					element[SPEEDY] -= 1;
				}
				element.y += element[DIRY] * element[SPEEDY]
			} else {
				element.y += element[SPEEDY]
			}

			element[SPEEDY] -= 0.1;
			if (element[SPEEDY] < 0) {
				if (element.y - hh <= 0) {
					element.y = hh;
					count++;
				}
			}


			// this.updateSpeed(element)

			// this.rollAction(element, pos)
		}
		if (count >= max) {
			this.showEgg()
		}
	}

	updateSpeed(element) {
		// if (element.y < this.box.height / 3) {
		if (element[SPEED] > 0) {
			element[SPEED] -= 1;
			if (element[SPEED] < 0) {
				element[SPEED] = 0;
			}
		}
		// }
	}

}